package com.yy.tech.data.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yy.tech.data.context.SecretContent;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonUtils {

    private static ObjectMapper mapper =null;

    private static ObjectMapper getMapper(){
        if (null == mapper){
            mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat(SecretContent.DATE_FORMAT));
//            mapper.enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
        }
        return mapper;
    }

    public static String toJson(Object obj){
        if (null == obj){
            return null;
        }
        try {
            return getMapper().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T fromJson(String json,Class<T> clz){
        if (StringUtils.hasLength(json)){
            try {
                return getMapper().readValue(json,clz);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public <T> T fromJson(String json, TypeReference<T> type){
        if (StringUtils.hasLength(json)){
            try {
                return getMapper().readValue(json,type);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

//    public boolean endByNumber(String str){
//        PatternMatchUtils.simpleMatch("\\w+\\d+",str);
//    }

    public static void main(String[] args) {
//        boolean m=PatternMatchUtils.simpleMatch("\\d+$","tbatch1");
        Pattern pattern = Pattern.compile("\\d+$");
        Matcher m=pattern.matcher("t_batch_12");
        System.out.println("result = "+m.find());
    }
}
